package edu.gzy.rpsm_property.pojo;

import com.github.pagehelper.Page;
import lombok.Data;
import lombok.NonNull;
import org.springframework.beans.MutablePropertyValues;

import javax.annotation.Nullable;
import javax.validation.constraints.NotNull;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Data
public class PageResult <T> {
    private final Integer pageNum;
    private final Integer pageSize;
    private final Long total;
    private final List<T> list;

    private PageResult(@NonNull Integer pageNum, @NonNull Integer pageSize, @NonNull Long total, @NonNull List<T> list) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.total = total;
        this.list = list;
    }

    public static <T> PageResult<T> restPage(@NonNull Integer
                                                     pageNum, @NotNull Integer pageSize, @NotNull Long total,
                                             @Nullable List<T> list) {
        return new PageResult<>(pageNum, pageSize, total, Optional.ofNullable(list).orElse(Collections.emptyList()));
    }

    /**
     * 将 MyBatis 分页结果转化为通用结果
     * 亚
     */
    public static <T> PageResult<T> restPage(@NotNull
                                             Page<T> page) {
        return new PageResult<>(page.getPageNum(),
                page.getPageSize(), page.getTotal(), page.getResult());
    }


}
